package com.book.service.impl;

import com.book.pojo.Channel;
import com.book.mapper.ChannelDao;
import com.book.service.ChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fk
 * @since 2023-10-11 22:54:58
 */
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelDao, Channel> implements ChannelService {
    @Autowired
    private ChannelDao channelDao;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<String> getAll() {
        Object o = redisTemplate.boundValueOps("channel").get();
        if(o==null){
            List<Channel> list = channelDao.selectList(null);
            List<String> names = new ArrayList<>();
            for (Channel channel : list) {
                names.add(channel.getName());
            }
            String ss = "";
            for(String s : names){
                ss += ","+s;
            }
            String str = ss.substring(1);
            redisTemplate.boundValueOps("channel").set(str);
            return names;
        }
        String st = (String) o;
        String[] srr = st.split(",");
        List<String> list2 = new ArrayList<>();
        for(String s : srr){
            list2.add(s);
        }
        return list2;
    }

    @Override
    public void addChannel(Channel channel) {
        redisTemplate.delete("channel");
        channelDao.insert(channel);
    }

    @Override
    public void updateChannel(Channel channel) {
        redisTemplate.delete("channel");
        channelDao.updateById(channel);
    }

    @Override
    public void deleteChannel(Integer id) {
        redisTemplate.delete("channel");
        channelDao.deleteById(id);
    }
}

